package at.technikumwien.trackmydevice.at.technikumwien.trackmydevice;

import java.util.Locale;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteQueryBuilder;
import android.location.Location;
import android.util.Log;

public class DataBaseHandler extends SQLiteOpenHelper {

	private static final String TAG = "PositionDatabase";
	private static final int DATABASE_VERSION = 2;
	private static final String KEY_WORD = "pos_logging";
	private static final String KEY_DEFINITION = "pos_logging";
	public static final String DATABASE_NAME = "position_log.db";
	public static String DATABASE_PATH = null;
	public static final String POSITION_TABLE_NAME = "position_log";
	public static final String HISTORY_TABLE_NAME = "logging_names";
			
	private SQLiteDatabase myDatabase;
	
	public DataBaseHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
		
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		CreateDBandDBTable(db);
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + POSITION_TABLE_NAME);
        onCreate(db);
	}
	
	private void CreateDBandDBTable (SQLiteDatabase db)
	{
		myDatabase = db;
		//CreateDataBase
		myDatabase = SQLiteDatabase.openDatabase(DATABASE_NAME, null, SQLiteDatabase.CREATE_IF_NECESSARY);
		myDatabase.setVersion(DATABASE_VERSION);
		myDatabase.setLocale(Locale.getDefault());
		myDatabase.setLockingEnabled(true);
		//Create Table with general logs
		myDatabase.execSQL("CREATE TABLE IF NOT EXISTS " + POSITION_TABLE_NAME
                + " (id integer AUTO_INCREMENT PRIMARY KEY, log_name varchar(100))"
                +";");
		//Create Table with locations if not existent
		myDatabase.execSQL("CREATE TABLE IF NOT EXISTS " + POSITION_TABLE_NAME
                + " (id integer AUTO_INCREMENT PRIMARY KEY, timestamp varchar(100), log_key integer, longitude varchar(100), latitude varchar(100), speed varchar(100), altitude varchar(100))"
                +";");
		DATABASE_PATH = myDatabase.getPath();
	}


	
}
